from bs4 import BeautifulSoup
import csv, os
os.chdir("D:/KIPRIS/Citation")

# csv file and csv object
f = open("citation_8417_2020.csv", "w", encoding="utf-8", newline="")
wf = csv.writer(f)

for year in range(1984, 2018):
	directory = "D:/KIPRIS/Citation/Citation{}.xml".format(year)
	file = open(directory, "r", encoding= "utf-8")
	txt = file.read()
	txt = txt.replace("\n", "")
	xml_list = txt.split('<?xml version="1.0" encoding="UTF-8"?>')
	for xml in xml_list[1:]:
		soup = BeautifulSoup(xml, 'xml')
		citations = soup.find_all("citationInfoV3")
		for citation in citations:
			citing = citation.find("ApplicationNumber").text
			cited_date = citation.find("StandardCitationLiteraturePublicationDate").text
			cited_code = citation.find("StandardCitationIdentificationCode").text
			cited_ctry = citation.find("StandardCitationLiteratureCountryCode").text
			cited_numb = citation.find("StandardCitationLiteraturenumber").text
			cited_type = citation.find("CitationLiteratureTypeCode").text
			cited_desc = citation.find("CitationLiteratureTypeCodeName").text
			line = [citing, cited_date, cited_code, cited_ctry, cited_numb, cited_type, cited_desc]
			wf.writerow(line)
			print(line)